<?php  
class Mtrangtinh extends CI_model{ 
     // đếm tổng số record trong table book 
    public function count_all(){ 
        return $this->db->count_all('trangtinh'); 
    }
    public function listall(){
        $query=$this->db->get("trangtinh");
        return $query->result_array();
    }
    // lấy dữ liệu theo từng phần 
    public function list_with_page($number, $offset){ 
        $this->db->order_by("trangtinh_id", "desc"); // order by ID
        $query =  $this->db->get('trangtinh',$number,$offset); 
        $result_array = $query->result_array();
        $i = 0;
        foreach ($result_array as $items) {
             $result_array[$i]['subTieuDe']   = substr($result_array[$i]['TieuDe'], 0, 100)."...";
             $result_array[$i]['subTomTat']   = substr($result_array[$i]['TomTat'], 0, 100)."...";
             $result_array[$i]['subNoiDung']  = substr($result_array[$i]['NoiDung'], 0, 100)."...";
             $result_array[$i]['subKeywords'] = substr($result_array[$i]['Keywords'], 0, 100)."...";

            $i++;
        }
        return $result_array; 
    } 
    /* function ListAll(){ 
        //$this->db->order_by("trangtinh_id","desc");
        $this->db->where("TinNoiBat",1);
        $query=$this->db->get('trangtinh');
        return $query->result_array();
    } */
    function Gettrangtinh($id){
        $this->db->where("trangtinh_id",$id);
        $query=$this->db->get('trangtinh');
        $data = $query->row();
        return $data; //only return  1 record
        //return $query->row_array();
    } 
	function GettrangtinhNoiBat(){
		$this->db->select('*');
		$this->db->from('trangtinh');
		$this->db->join('theloai', 'theloai.theloai_id = trangtinh.id_TheLoai');
		$this->db->where("TinNoiBat",1);
		$query=$this->db->get();
        return $query->result_array();
        //return $query->row_array();
    } 

    function GetTrangTinhByTenTrangTinh($tentrang){
        $this->db->select('*');
		$this->db->from('trangtinh');
		$this->db->where("trangtinh.TenTrangTinh",$tentrang);
        $query=$this->db->get();
        return $query->row();
    }
	function GettrangtinhByNhieuTheLoai($arr_id_theloai){
		$this->db->select('*');
		$this->db->from('trangtinh');
		$this->db->join('theloai', 'theloai.theloai_id = trangtinh.id_TheLoai');
		$this->db->join('quantri', 'quantri.id_quantri = trangtinh.id_user');
        $this->db->where_in("id_theloai",$arr_id_theloai);
		$query=$this->db->get();
        return $query->result_array();
    }
    function GettrangtinhByTheLoai($id_theloai){
	
        $this->db->where("id_theloai",$id_theloai);
        $query=$this->db->get('trangtinh');
        return $query->result_array();
    }
    function Insert($data) {
        $this->db->insert('trangtinh',$data);
        return $this->db->insert_id();
    }

    public function Update($data, $trangtinh_id) {
        $this->db->where('trangtinh_id', $trangtinh_id);
        $this->db->update('trangtinh', $data); 
    }

    function Delete($trangtinh_id){
        $this->db->where("trangtinh_id",$trangtinh_id);
        $this->db->delete('trangtinh');
        //unlink("./upload/" . $file[0]['file_name']); //xóa file upload trong thư mục chứa
        return $this->db->affected_rows();     
    }
}
?>